﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.SystemManage
{
    public partial class ComListSendPwd : BasePage
    {
        public ComListSendPwd()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.企业管理.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();
            }
        }

        public void SetBind()
        {
            int totalRecord = 0;
            string strWhere = "";
            if (!string.IsNullOrEmpty(this.txtCode.Text.Trim()))
            {
                strWhere += "and companyid='" + this.txtCode.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(this.txtCompanyName.Text.Trim()))
            {
                strWhere += "and companyName like '%"+this.txtCompanyName.Text.Trim()+"%'";
            }
            if (!string.IsNullOrEmpty(this.ddlComType.SelectedValue))
            {
                strWhere += "and CompanyType='"+this.ddlComType.SelectedValue+"'";
            }
            if (!string.IsNullOrEmpty(this.dllIsSend.SelectedValue))
            {
                strWhere += "and ISSend ='"+this.dllIsSend.SelectedValue+"'";
            }
            strWhere += "order by CompanyId";
            this.gvwComList.DataSource = HZ.DrugTrade.BLLFactory.Factory.BLL_Company().GetCompanySendByWhere(strWhere, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalRecord);
            this.AspNetPager1.RecordCount = totalRecord;
            this.gvwComList.DataBind();
        }
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            SetBind();
        }

        protected void gvwComList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton lbnPrint = (LinkButton)e.Row.FindControl("lblPrint");
                //Label lbnPrint = (Label)e.Row.FindControl("lblPrint");
                string accountId = gvwComList.DataKeys[e.Row.RowIndex].Values[0].ToString();
                string status = "0";
                if (gvwComList.DataKeys[e.Row.RowIndex].Values[1] != null)
                {
                    if (!string.IsNullOrEmpty(gvwComList.DataKeys[e.Row.RowIndex].Values[1].ToString()))
                    {
                        status = gvwComList.DataKeys[e.Row.RowIndex].Values[1].ToString().ToLower();
                    }
                }

                //if (status == "0")
                //{
                    //lbnPrint.PostBackUrl = "ComPrint.aspx?accountId=" + accountId;
                    //lbnPrint.ToolTip = "进入打印信息";
                    //lbnPrint.Text = string.Format(" <a href=\"javascript:PrintInfo('{0}')\">进入打印信息</a>", accountId);
                //}
                //else if (status == "false" && !IsBid_Landing.Equals("1"))
                //{
                //    lbnPrint.Text = string.Format("<SPAN style='color:red'>被禁止登陆企业,不能发放密码！</SPAN>", accountId);
                //}
                //else if (status == "1")
                //{
                //    lbnPrint.Text = string.Format("<SPAN style='color:green'>已经发放</SPAN>", accountId);
                //}
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/RegCompany.aspx");
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            if (!string.IsNullOrEmpty(this.txtCode.Text.Trim()))
            {
                strWhere += "and companyid='" + this.txtCode.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(this.txtCompanyName.Text.Trim()))
            {
                strWhere += "and companyName like '%" + this.txtCompanyName.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(this.ddlComType.SelectedValue))
            {
                strWhere += "and CompanyType='" + this.ddlComType.SelectedValue + "'";
            }
            if (!string.IsNullOrEmpty(this.dllIsSend.SelectedValue))
            {
                strWhere += "and ISSend ='" + this.dllIsSend.SelectedValue + "'";
            }
            strWhere += "order by CompanyId";
            string sql = string.Format(@"SELECT companyid 用户名称,companyname 企业名称,case companytype when 1 then '经营企业' when 2 then '配送企业' when 3 then '生产企业直接配送' when 4 then '经营企业直接配送'
                                             end as 企业类型,SendOperator 发放人,case IsSend when 0 then '未领取' else '已领取' end as 领取状态,SendDate 领取时间  FROM View_CompanySendPwd WHERE 1=1 {0}", strWhere);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "发放用户名密码.xls");
        }

    }
}
